package luke.ui.components;

import luke.ui.dialogs.DonationDialog;
import luke.ui.dialogs.UpdateChecker;
import luke.core.utils.SvgIconLoader;
import luke.core.config.AppDataManager;
import luke.core.utils.LanguageManager; // 新增：导入LanguageManager

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.net.URL;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Locale; // 新增：导入Locale类用于语言设置

/**
 * 菜单栏类
 * 负责创建和管理应用程序的顶部菜单栏
 */
public class MenuBar extends JMenuBar {
    private JFrame parentFrame;
    // 定义暗色主题的颜色
    private static final Color DARK_BG = new Color(60, 60, 60);
    private static final Color DARK_HOVER_BG = new Color(80, 80, 80);
    private static final Color DARK_PRESSED_BG = new Color(100, 100, 100);
    private static final Color DARK_TEXT = new Color(220, 220, 220);
    
    // 新增：语言设置相关
    private AppDataManager appDataManager;
    private JMenu languageMenu;
    private ButtonGroup languageGroup;
    private JMenuItem chineseItem, traditionalChineseItem, englishItem, koreanItem, japaneseItem, arabicItem;
    private LanguageManager languageManager; // 新增：语言管理器

    public MenuBar(JFrame parentFrame) {
        this.parentFrame = parentFrame;
        this.appDataManager = AppDataManager.getInstance(); // 获取AppDataManager实例
        this.languageManager = LanguageManager.getInstance(); // 获取LanguageManager实例
        createMenuBar();
        loadLanguagePreference(); // 加载语言偏好设置
    }

    private void createMenuBar() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); // 添加边距使文字居中

        // 新增：创建语言菜单
        createLanguageMenu();

        JButton donateButton = createMenuButton(languageManager.getMenuText("donate"), e -> showDonationDialogFromMenu());

        JButton updateButton = createMenuButton(languageManager.getMenuText("check.update"), e -> new UpdateChecker(parentFrame, true).checkForUpdates());

        JButton helpButton = createMenuButton(languageManager.getMenuText("help"), e -> openHelp());

        JButton contactButton = createMenuButton(languageManager.getMenuText("contact"), e -> contactUs());

        add(languageMenu); // 添加语言菜单
        add(donateButton);
        add(updateButton);
        add(helpButton);
        add(contactButton);
    }

    // 新增：创建语言菜单
    private void createLanguageMenu() {
        languageMenu = new JMenu(languageManager.getLanguageText("setting"));
        languageMenu.setBorderPainted(false);
        languageMenu.setFocusPainted(false);
        languageMenu.setContentAreaFilled(true);
        languageMenu.setOpaque(true);
        languageMenu.setBackground(DARK_BG);
        languageMenu.setForeground(DARK_TEXT);
        languageMenu.setMargin(new Insets(0, 5, 0, 5)); // 添加边距使文字居中
        
        // 强制设置菜单字体颜色（解决可能的颜色显示不一致问题）
        languageMenu.setFont(new Font(languageMenu.getFont().getName(), Font.PLAIN, languageMenu.getFont().getSize()));
        
        languageGroup = new ButtonGroup();
        
        // 创建语言选项
        chineseItem = createLanguageMenuItem(languageManager.getLanguageText("chinese"), "zh");
        traditionalChineseItem = createLanguageMenuItem(languageManager.getLanguageText("traditional.chinese"), "zh_TW");
        englishItem = createLanguageMenuItem(languageManager.getLanguageText("english"), "en");
        koreanItem = createLanguageMenuItem(languageManager.getLanguageText("korean"), "ko");
        japaneseItem = createLanguageMenuItem(languageManager.getLanguageText("japanese"), "ja");
        arabicItem = createLanguageMenuItem(languageManager.getLanguageText("arabic"), "ar");
        
        languageMenu.add(chineseItem);
        languageMenu.add(traditionalChineseItem);
        languageMenu.add(englishItem);
        languageMenu.add(koreanItem);
        languageMenu.add(japaneseItem);
        languageMenu.add(arabicItem);
        
        // 悬停效果
        languageMenu.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                languageMenu.setBackground(DARK_HOVER_BG);
            }

            public void mouseExited(MouseEvent e) {
                languageMenu.setBackground(DARK_BG);
            }

            public void mousePressed(MouseEvent e) {
                languageMenu.setBackground(DARK_PRESSED_BG);
            }

            public void mouseReleased(MouseEvent e) {
                languageMenu.setBackground(DARK_HOVER_BG);
            }
        });
    }

    // 新增：创建语言菜单项
    private JMenuItem createLanguageMenuItem(String text, String languageCode) {
        JRadioButtonMenuItem item = new JRadioButtonMenuItem(text);
        item.setBorderPainted(false);
        item.setFocusPainted(false);
        item.setContentAreaFilled(true);
        item.setOpaque(true);
        item.setBackground(DARK_BG);
        item.setForeground(DARK_TEXT);
        item.setMargin(new Insets(0, 5, 0, 5)); // 添加边距使文字居中
        
        // 强制设置菜单项字体颜色（解决可能的颜色显示不一致问题）
        item.setFont(new Font(item.getFont().getName(), Font.PLAIN, item.getFont().getSize()));
        
        item.setActionCommand(languageCode);
        item.addActionListener(e -> setLanguage(languageCode));
        
        languageGroup.add(item);
        
        // 悬停效果
        item.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                item.setBackground(DARK_HOVER_BG);
            }

            public void mouseExited(MouseEvent e) {
                item.setBackground(DARK_BG);
            }

            public void mousePressed(MouseEvent e) {
                item.setBackground(DARK_PRESSED_BG);
            }

            public void mouseReleased(MouseEvent e) {
                item.setBackground(DARK_HOVER_BG);
            }
        });
        
        return item;
    }

    // 新增：设置语言
    private void setLanguage(String languageCode) {
        // 保存语言偏好设置
        languageManager.setLanguage(languageCode);
        
        // 显示提示信息
        JOptionPane.showMessageDialog(parentFrame, 
            languageManager.getString("language.changed"), 
            languageManager.getString("language.changed.title"), 
            JOptionPane.INFORMATION_MESSAGE);
    }

    // 新增：加载语言偏好设置
    private void loadLanguagePreference() {
        String currentLanguage = languageManager.getCurrentLanguage();
        
        // 根据当前语言选择对应的菜单项
        switch (currentLanguage) {
            case "zh":
                chineseItem.setSelected(true);
                break;
            case "zh_TW":
                traditionalChineseItem.setSelected(true);
                break;
            case "en":
                englishItem.setSelected(true);
                break;
            case "ko":
                koreanItem.setSelected(true);
                break;
            case "ja":
                japaneseItem.setSelected(true);
                break;
            case "ar":
                arabicItem.setSelected(true);
                break;
            default:
                // 默认使用英文
                englishItem.setSelected(true);
                break;
        }
    }

    private JButton createMenuButton(String text, ActionListener listener) {
        JButton button = new JButton(text);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setContentAreaFilled(true); // 改为true以允许背景填充
        button.setOpaque(true);
        button.setBackground(DARK_BG); // 使用暗色背景
        button.setForeground(DARK_TEXT); // 使用浅色文字
        button.setMargin(new Insets(0, 5, 0, 5));
        button.addActionListener(listener);

        // 悬停效果
        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(DARK_HOVER_BG); // 悬停时的背景色
            }

            public void mouseExited(MouseEvent e) {
                button.setBackground(DARK_BG); // 恢复默认背景色
            }

            public void mousePressed(MouseEvent e) {
                button.setBackground(DARK_PRESSED_BG); // 点击时的背景色
            }

            public void mouseReleased(MouseEvent e) {
                button.setBackground(DARK_HOVER_BG); // 释放时恢复悬停背景色
            }
        });

        return button;
    }

    private void showDonationDialogFromMenu() {
        // 菜单栏捐赠按钮按下后，直接调用捐赠的弹窗
        SwingUtilities.invokeLater(() -> {
            DonationDialog donationDialog = new DonationDialog(parentFrame, "menu");
            donationDialog.setVisible(true);
        });
    }

    // 打开帮助文档
    private void openHelp() {
        try {
            // 使用类加载器获取资源文件的URL
            URL helpUrl = getClass().getClassLoader().getResource("help.html");

            if (helpUrl != null) {
                // 总是提取到临时文件
                File tempFile = extractResourceToTempFile(helpUrl, "help", ".html");
                
                // 直接使用Chrome命令行参数打开文件，避免参数污染
                openFileWithChrome(tempFile.getAbsolutePath());
            } else {
                // 如果资源文件不存在，尝试打开在线帮助
                openOnlineHelp();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(parentFrame, "无法打开帮助文档: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    private File extractResourceToTempFile(URL resourceUrl, String prefix, String suffix) throws IOException {
        System.out.println("开始提取资源到临时文件: " + resourceUrl.toString());
        
        // 使用AppDataManager的临时目录创建临时文件
        Path tempDir = AppDataManager.getInstance().getTempDirectory();
        String tempFileName = prefix + System.currentTimeMillis() + suffix;
        Path tempPath = tempDir.resolve(tempFileName);
        File tempFile = tempPath.toFile();
        System.out.println("临时文件路径: " + tempFile.getAbsolutePath());

        // 从资源URL复制内容到临时文件
        try (InputStream is = resourceUrl.openStream();
                FileOutputStream fos = new FileOutputStream(tempFile)) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        }
        
        System.out.println("资源提取完成");

        return tempFile;
    }

    /**
     * 显示手动打开文件的对话框
     * @param filePath 文件路径
     */
    private void showManualOpenDialog(String filePath) {
        // 清理文件路径
        String cleanPath = filePath;
        
        // 移除可能的浏览器参数
        if (cleanPath.contains("--disable-background-networking")) {
            int paramIndex = cleanPath.indexOf("--disable-background-networking");
            if (paramIndex > 0) {
                cleanPath = cleanPath.substring(0, paramIndex).trim();
            }
        }
        
        // 移除可能的其他参数
        if (cleanPath.contains("%20d/")) {
            int paramIndex = cleanPath.indexOf("%20d/");
            if (paramIndex > 0) {
                cleanPath = cleanPath.substring(0, paramIndex).trim();
            }
        }
        
        // URL解码处理可能的编码问题
        try {
            cleanPath = java.net.URLDecoder.decode(cleanPath, "UTF-8");
        } catch (Exception e) {
            // 如果解码失败，使用原始路径
        }
        
        // 如果是file协议，提取实际文件路径
        if (cleanPath.startsWith("file:")) {
            if (cleanPath.startsWith("file:///")) {
                cleanPath = cleanPath.substring(8); // 移除 "file:///"
            } else if (cleanPath.startsWith("file:/")) {
                cleanPath = cleanPath.substring(6); // 移除 "file:/"
            }
        }
        
        // 创建一个引导HTML文件
        try {
            Path tempDir = AppDataManager.getInstance().getTempDirectory();
            String tempFileName = "open_file_guide_" + System.currentTimeMillis() + ".html";
            Path tempPath = tempDir.resolve(tempFileName);
            File tempFile = tempPath.toFile();
            
            // 写入引导HTML内容
            String htmlContent = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>文件打开引导</title>\n" +
                "    <style>\n" +
                "        body { font-family: Arial, sans-serif; margin: 50px; }\n" +
                "        .container { max-width: 800px; margin: 0 auto; }\n" +
                "        .file-path { background: #f0f0f0; padding: 15px; border-radius: 5px; word-break: break-all; }\n" +
                "        .button { background: #4CAF50; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block; margin: 10px 0; }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <h2>文件打开引导</h2>\n" +
                "        <p>请点击下面的链接打开目标文件：</p>\n" +
                "        <a href=\"file:///" + cleanPath.replace("\\", "/") + "\" class=\"button\">打开目标文件</a>\n" +
                "        <p>或者复制以下路径到浏览器地址栏中打开：</p>\n" +
                "        <div class=\"file-path\">" + cleanPath + "</div>\n" +
                "        <p><small>提示：如果链接无法打开，请确保文件路径正确且文件存在。</small></p>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
            
            try (java.io.FileWriter writer = new java.io.FileWriter(tempFile)) {
                writer.write(htmlContent);
            }
            
            // 打开引导文件
            if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                Desktop.getDesktop().browse(tempFile.toURI());
            } else {
                // 如果桌面操作不支持，显示路径信息
                JOptionPane.showMessageDialog(parentFrame,
                    "请手动打开以下文件：\n" + cleanPath,
                    "文件路径",
                    JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception e) {
            // 如果创建引导文件失败，直接显示路径
            JOptionPane.showMessageDialog(parentFrame,
                "请手动打开以下文件：\n" + cleanPath,
                "文件路径",
                JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /**
     * 使用系统命令打开URL
     * @param url 要打开的URL
     * @return 是否成功执行命令
     */
    private boolean openUrlWithSystemCommand(String url) {
        try {
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows系统 - 使用cmd start命令直接打开URL
                // 使用explorer而不是rundll32避免Chrome参数污染问题
                String[] cmd = new String[]{"cmd", "/c", "start", "\"\"", "\"" + url + "\""};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
            } else if (os.contains("mac")) {
                // macOS系统
                String[] cmd = new String[]{"open", url};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
            } else if (os.contains("nix") || os.contains("nux")) {
                // Linux系统
                String[] cmd = new String[]{"xdg-open", url};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
            } else {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 尝试启动浏览器并打开URL
     * @param url 要打开的URL
     * @return 是否成功启动浏览器并打开URL
     */
    private boolean launchBrowserAndOpenUrl(String url) {
        try {
            // 清理URL，移除可能的浏览器参数
            String cleanUrl = url;
            
            // 移除可能的浏览器参数（如 --disable-background-networking）
            if (cleanUrl.contains("--disable-background-networking")) {
                int paramIndex = cleanUrl.indexOf("--disable-background-networking");
                if (paramIndex > 0) {
                    cleanUrl = cleanUrl.substring(0, paramIndex).trim();
                }
            }
            
            // 移除可能的其他参数
            if (cleanUrl.contains("%20d/")) {
                int paramIndex = cleanUrl.indexOf("%20d/");
                if (paramIndex > 0) {
                    cleanUrl = cleanUrl.substring(0, paramIndex).trim();
                }
            }
            
            // URL解码处理可能的编码问题
            try {
                cleanUrl = java.net.URLDecoder.decode(cleanUrl, "UTF-8");
            } catch (Exception e) {
                // 如果解码失败，使用原始URL
            }
            
            // 如果是file协议，提取实际文件路径
            if (cleanUrl.startsWith("file:")) {
                if (cleanUrl.startsWith("file:///")) {
                    cleanUrl = cleanUrl.substring(8); // 移除 "file:///"
                } else if (cleanUrl.startsWith("file:/")) {
                    cleanUrl = cleanUrl.substring(6); // 移除 "file:/"
                }
            }
            
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows系统 - 先打开空白页确保浏览器启动，再打开文件
                try {
                    // 先启动浏览器打开空白页
                    String[] cmd1 = new String[]{"cmd", "/c", "start", "\"\"", "\"about:blank\""};
                    Process process1 = Runtime.getRuntime().exec(cmd1);
                    process1.waitFor(1, java.util.concurrent.TimeUnit.SECONDS);
                    
                    // 等待一小段时间确保浏览器启动
                    Thread.sleep(500);
                    
                    // 再打开实际文件
                    String[] cmd2 = new String[]{"cmd", "/c", "start", "\"\"", "\"" + cleanUrl + "\""};
                    Process process2 = Runtime.getRuntime().exec(cmd2);
                    process2.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
                } catch (Exception e) {
                    // 如果上述方法失败，回退到简单方法
                    String[] cmd = new String[]{"cmd", "/c", "start", "\"\"", "\"" + cleanUrl + "\""};
                    Process process = Runtime.getRuntime().exec(cmd);
                    process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
                }
                return true;
            } else if (os.contains("mac")) {
                // macOS系统
                String[] cmd = new String[]{"open", cleanUrl};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
                return true;
            } else if (os.contains("nix") || os.contains("nux")) {
                // Linux系统
                String[] cmd = new String[]{"xdg-open", cleanUrl};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 打开在线帮助
     */
    private void openOnlineHelp() {
        try {
            JOptionPane.showMessageDialog(parentFrame,
                    "本地帮助文档不存在，正在打开在线帮助文档...",
                    "提示",
                    JOptionPane.INFORMATION_MESSAGE);
            
            String onlineUrl = "https://www.runbeats.com/help";
            
            // 尝试打开在线帮助
            if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                Desktop.getDesktop().browse(URI.create(onlineUrl));
            } else {
                // 如果桌面操作不支持，尝试使用系统命令
                openUrlWithSystemCommand(onlineUrl);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(parentFrame, "无法打开在线帮助: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * 打开在线联系方式
     */
    private void openOnlineContact() {
        try {
            JOptionPane.showMessageDialog(parentFrame,
                    "本地联系页面不存在，正在打开官网...",
                    "提示",
                    JOptionPane.INFORMATION_MESSAGE);
            
            String onlineUrl = "https://www.runbeats.com/send";
            
            // 尝试打开在线联系方式
            if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                Desktop.getDesktop().browse(URI.create(onlineUrl));
            } else {
                // 如果桌面操作不支持，尝试使用系统命令
                openUrlWithSystemCommand(onlineUrl);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(parentFrame, "无法打开在线联系方式: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    // 打开联系我们页面
    private void contactUs() {
        try {
            // 使用类加载器获取资源文件的URL
            URL contactUrl = getClass().getClassLoader().getResource("send.html");

            if (contactUrl != null) {
                // 总是提取到临时文件
                File tempFile = extractResourceToTempFile(contactUrl, "contact", ".html");
                
                // 直接使用Chrome命令行参数打开文件，避免参数污染
                openFileWithChrome(tempFile.getAbsolutePath());
            } else {
                // 如果资源文件不存在，尝试打开在线联系方式
                openOnlineContact();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(parentFrame, "无法打开联系方式: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * 直接使用Chrome打开文件，避免参数污染问题
     * @param filePath 文件路径
     */
    private void openFileWithChrome(String filePath) {
        try {
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows系统 - 尝试直接调用Chrome打开文件
                File chromePath = findChromePath();
                if (chromePath != null && chromePath.exists()) {
                    // 使用Chrome直接打开文件，避免参数污染
                    String[] cmd = new String[]{
                        chromePath.getAbsolutePath(), 
                        "--new-window",  // 强制在新窗口中打开
                        "file:///" + filePath.replace("\\", "/")
                    };
                    Process process = Runtime.getRuntime().exec(cmd);
                } else {
                    // 如果找不到Chrome，回退到系统默认方式
                    String fileUrl = "file:///" + filePath.replace("\\", "/");
                    openUrlWithSystemCommand(fileUrl);
                }
            } else {
                // 其他系统使用系统默认方式
                String fileUrl = "file:///" + filePath.replace("\\", "/");
                openUrlWithSystemCommand(fileUrl);
            }
        } catch (Exception e) {
            // 如果直接调用Chrome失败，回退到系统默认方式
            try {
                String fileUrl = "file:///" + filePath.replace("\\", "/");
                openUrlWithSystemCommand(fileUrl);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 查找Chrome可执行文件路径
     * @return Chrome可执行文件路径，如果找不到返回null
     */
    private File findChromePath() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            // Windows系统常见的Chrome安装路径
            String[] possiblePaths = {
                System.getenv("PROGRAMFILES") + "\\Google\\Chrome\\Application\\chrome.exe",
                System.getenv("PROGRAMFILES(X86)") + "\\Google\\Chrome\\Application\\chrome.exe",
                System.getenv("LOCALAPPDATA") + "\\Google\\Chrome\\Application\\chrome.exe",
                "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
                "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
            };
            
            for (String path : possiblePaths) {
                File chrome = new File(path);
                if (chrome.exists()) {
                    return chrome;
                }
            }
        }
        return null;
    }

    /**
     * 启动浏览器打开指定网址
     * @param url 要打开的网址
     */
    private void launchBrowserWithUrl(String url) {
        try {
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows系统 - 启动浏览器打开指定网址
                String[] cmd = new String[]{"cmd", "/c", "start", "\"\"", "\"" + url + "\""};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(1, java.util.concurrent.TimeUnit.SECONDS);
            } else if (os.contains("mac")) {
                // macOS系统
                String[] cmd = new String[]{"open", url};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(1, java.util.concurrent.TimeUnit.SECONDS);
            } else if (os.contains("nix") || os.contains("nux")) {
                // Linux系统
                String[] cmd = new String[]{"xdg-open", url};
                Process process = Runtime.getRuntime().exec(cmd);
                process.waitFor(1, java.util.concurrent.TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            // 忽略启动浏览器的异常
            e.printStackTrace();
        }
    }

}